//根据二叉树创建字符串(力扣606)
  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }

class Solution {
    public String tree2str(TreeNode root) {
        StringBuilder res=new StringBuilder();
        tree2strChild(root,res);
        return res.toString();
    }
    public static void tree2strChild(TreeNode root,StringBuilder stringBuilder){
        if(root==null){
            return;
        }
        stringBuilder.append(root.val);

        //左子树
        if(root.left!=null){
            stringBuilder.append('(');
            tree2strChild(root.left,stringBuilder);
            stringBuilder.append(')');
        }else{
            if(root.right!=null){
                stringBuilder.append("()");
            }else{
                return;
            }
        }
        //右子树
        if(root.right!=null){
            stringBuilder.append('(');
            tree2strChild(root.right,stringBuilder);
            stringBuilder.append(')');
        }else{
            return;
        }
    }
}